package com.yiwei.zhh.fragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.yiwei.zhh.activity.HelpDetailActivity;
import com.yiwei.zhh.R;
import com.yiwei.zhh.adapter.HelpAdapter;
import com.yiwei.zhh.api.callback.HelpContract;
import com.yiwei.zhh.api.requestInfo.HelpRequest;
import com.yiwei.zhh.constant.Const;
import com.yiwei.zhh.interfaces.OnRecyclerViewListener;
import com.yiwei.zhh.model.ContentDetailModel;
import com.yiwei.zhh.model.HelpModel;
import com.yiwei.zhh.view.CircularProgress;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

/**
 * 帮助界面
 * Created by MasterDong on 16/8/11.
 */
public class HelpFragment extends BaseFragment implements HelpContract.View {


    private List<ContentDetailModel> list = new ArrayList<ContentDetailModel>();
    private String type = "";
    private RecyclerView recyclerView;
    private HelpAdapter adapter = null;
    private CircularProgress loading;
    private HelpRequest api;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_help;
    }

    @Override
    protected void initView() {
        api = HelpRequest.create();
        api.attachView(this);
        loading = (CircularProgress) root.findViewById(R.id.progressBar);
        recyclerView = (RecyclerView) root.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);

        if (list.size() > 0) {
            adapter = new HelpAdapter(getActivity(), list);
            recyclerView.setAdapter(adapter);
            adapter.setOnRecyclerViewListener(new myOnRecyclerViewListener());
        } else
            getHelp();
    }


    /**
     * 获取帮助信息
     */
    private void getHelp() {
        api.getHelp();
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        showLoading(false);
    }

    @Override
    public void onRequestStart() {
        showLoading(true);
    }

    @Override
    public void getHelpSuccess(HelpModel model) {
        setData(model);
    }


    /**
     * 列表点击事件监听
     */
    class myOnRecyclerViewListener implements OnRecyclerViewListener {

        @Override
        public void onItemClick(List<?> list, int position) {
            List<ContentDetailModel> l = (List<ContentDetailModel>) list;

            Intent intent = new Intent();
            intent.putExtra("id", l.get(position).getId());
            intent.putExtra("type", Const.FragmentType.HELP);
            intent.setClass(getActivity(), HelpDetailActivity.class);
            startActivity(intent);
            getActivity().overridePendingTransition(R.anim.right_enter, R.anim.left_out);
        }

        @Override
        public boolean onItemLongClick(List<?> list, int position) {
            return false;
        }
    }


    /**
     * 是否显示加载框
     *
     * @param b
     */
    private void showLoading(boolean b) {
        if (b) {
            if (loading != null) loading.setVisibility(View.VISIBLE);
        } else {
            if (loading != null) loading.setVisibility(View.GONE);
        }
    }

    /**
     * 设置数据
     */
    private void setData(HelpModel model) {
        try {
            list.clear();
            for (int i = 0; i < model.getData().size(); i++) {
                for (int j = 0; j < model.getData().get(i).getList().size(); j++) {
                    ContentDetailModel helpModel = new ContentDetailModel();
                    helpModel.setId(model.getData().get(i).getList().get(j).getId());
                    helpModel.setTitle(model.getData().get(i).getList().get(j).getTitle());
                    if (!type.equals(model.getData().get(i).getTitle())) {
                        type = model.getData().get(i).getTitle();
                        helpModel.setType(type);
                    }
                    list.add(helpModel);
                }
            }

            adapter = new HelpAdapter(getActivity(), list);
            recyclerView.setAdapter(adapter);
            adapter.setOnRecyclerViewListener(new myOnRecyclerViewListener());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("HelpFragment");
    }


    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("HelpFragment");
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        api.detachView();
        api = null;
    }
}
